package com.zhenyi.jiahua.index;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.cenjiawen.common.IndexController;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jiahua.common.model.JhStaff;
import com.utils.AuthUtils;
import com.utils.DateUtils;
import com.zhenyi.jiahua.interceptor.AuthInterceptor;
import com.zhenyi.jiahua.interceptor.MsgInterceptor;
import com.zhenyi.jiahua.interceptor.PermissionInterceptor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

@Before({ AuthInterceptor.class, PermissionInterceptor.class, MsgInterceptor.class })
public class JHController extends IndexController {

	private final String REPLACE_MARK = "＇";

	protected Integer getPageSize() {
		int pageSize = getParaToInt("pageSize", PAGE_SIZE);
		setAttr("pageSize", pageSize);
		return pageSize;
	}

	@Override
	public String getPara(String name, String defaultValue) {
		String str = super.getPara(name, defaultValue);
		if (str == null) {
			return str;
		}
		return str.replace("'", REPLACE_MARK);
	}

	@Override
	public String getPara(int index) {
		String str = super.getPara(index);
		if (str == null) {
			return str;
		}
		return str.replace("'", REPLACE_MARK);
	}

	@Override
	public String getPara(String name) {
		String str = super.getPara(name);
		if (str == null) {
			return str;
		}
		return trimString(str);
	}

	@Override
	public String getPara(int index, String defaultValue) {
		String str = super.getPara(index, defaultValue);
		if (str == null) {
			return str;
		}
		str = StrUtil.trim(str);
		return str.replace("'", REPLACE_MARK);
	}

	public String getParaRelaceN(String name) {
		String value = getPara(name, "");
		return trimStringN(value);
	}

	protected String trimStringN(String value) {
		if (StrKit.isBlank(value)) {
			return "";
		}
		value = StrUtil.trim(value);
		value = value.replace("\n", "<br/>");
		value = value.replace("\t", " ");
		value = value.replace("'", REPLACE_MARK);
		return value;
	}

	protected String trimString(String value) {
		if (StrKit.isBlank(value)) {
			return "";
		}
		value = StrUtil.trim(value);
		value = value.replace("\n", " ");
		value = value.replace("\t", " ");
		value = value.replace("'", REPLACE_MARK);
		return value;
	}

	public String getParaTrim(String name) {
		String value = getPara(name, "");
		return trimString(value);
	}

	protected final static Integer YES = 1;

	private static String htmlEncode(String source) {
		if (source == null) {
			return "";
		}
		String html = "";
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < source.length(); i++) {
			char c = source.charAt(i);
			switch (c) {
			case '<':
				buffer.append("<");
				break;
			case '>':
				buffer.append(">");
				break;
			case '&':
				buffer.append("&");
				break;
			case '"':
				buffer.append("\"");
				break;
			case 10:
			case 13:
				break;
			default:
				buffer.append(c);
			}
		}
		html = buffer.toString();
		return html;
	}

	/**
	 * 生成转义的json
	 * 
	 * @param object
	 * @return
	 * @Time 2018年10月22日 下午9:43:04
	 */
	protected String jsonObject(Object object) {
		String json = JsonKit.toJson(object);
		// System.out.println(json);
		// json = json.replace("'", "\'");
		String str = ReUtil.escape(json);
		// System.out.println(str);
		// str = StringEscapeUtils.escapeHtml(str);
		// System.out.println(str);
		// System.out.println(StringEscapeUtils.unescapeHtml4("\"'"));
		// System.out.println(htmlEncode("\"'"));
		// StringEscapeUtils.escapeHtml(HTMLText)
		return str;
	}

	/**
	 * 获取登录员工
	 * 
	 * @return
	 */
	public JhStaff getStaff() {
		return JhStaff.dao.findById(Long.valueOf(AuthUtils.getTokenUid(this)));
	}

	protected void setRainbow(Page<?> pages) {
		Integer requestPage = getParaToInt("page");

		HttpServletRequest request = getRequest();
		String url = request.getRequestURL().toString();
		String queryurl = request.getQueryString();
		if (StrKit.notBlank(queryurl)) {
			if (requestPage != null) {
				queryurl = queryurl.replace("&page=" + requestPage, "");
				queryurl = queryurl.replace("page=" + requestPage, "");
				if (StrKit.notBlank(queryurl)) {
					url = url + "?" + queryurl + "&page=";
				} else {
					url = url + "?page=";
				}
			} else {
				url = url + "?" + queryurl + "&page=";
			}
		} else {
			url = url + "?page=";
		}
		int pageTotal = pages.getTotalPage();
		int pageTotalRow = pages.getTotalRow();
		int page = pages.getPageNumber();
		int[] rainbow = PageUtil.rainbow(pages.getPageNumber(), pageTotal);
		setAttr("rainbow", rainbow);
		setAttr("pageUrl", url);
		setAttr("pageTotalRow", pageTotalRow);
		setAttr("pageSize", pages.getPageSize());
		setAttr("pageCurrent", page);
	}

	/**
	 * 获取date 日期
	 * 
	 * @param name
	 * @return
	 */
	protected Date getTimeDate(String name) {
		try {
			String date = getPara(name);
			if (StrKit.isBlank(date)) {
				return null;
			}
			Date dt = DateUtils.dateFromString(date, DateUtils.YYYYMMDD_HHMMSS);
			return dt;
		} catch (Exception e) {
			return null;
		}
	}

	protected Date getTimeDate(String name, String format) {
		try {
			String date = getPara(name);
			if (StrKit.isBlank(date)) {
				return null;
			}
			Date dt = DateUtils.dateFromString(date, format);
			return dt;
		} catch (Exception e) {
			return null;
		}
	}

	protected String getStartToday() {
		String date = getPara("startDate", DateUtil.today() + " 00:00:00");
		setAttr("startDate", date);
		return date;
	}

	protected String getEndToday() {
		String date = getPara("endDate", DateUtil.today() + " 23:59:59");
		setAttr("endDate", date);
		return date;
	}

	protected String getRequestUrl() {
		Controller controller = this;
		String url = controller.getRequest().getRequestURL().toString();
		String queryurl = controller.getRequest().getQueryString();
		if (StrKit.notBlank(queryurl)) {
			url = url + "?" + queryurl;
		}
		return url;
	}

	@Override
	public void info() {
		setRequestUrl();
		// Enumeration<String> atts = getRequest().getHeaderNames();
		// while (atts.hasMoreElements()) {
		// String string = (String) atts.nextElement();
		// System.out.println(string);
		// }
		System.out.println(getRequest().getHeader("Referer"));
	}

	/**
	 * 当前请求页面 requestUrl
	 * 
	 * @Time 2018年11月24日 上午10:40:43
	 */
	protected void setRequestUrl() {
		String requestUrl = getRequestUrl();
		setAttr("requestUrl", requestUrl);
	}

	/**
	 * 跳转过来的页面 referer
	 * 
	 * @Time 2018年11月24日 上午10:40:29
	 */
	protected void setReferer() {
		setAttr("referer", getRequest().getHeader("Referer"));
	}
}
